#include <iostream>
#include <cstring>
#include <fstream>
#include <cmath>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <ctime>
#include <conio.h>

using namespace std;
void Create_Probability();//creates the probabilty of each population

void Create_RAND_MUTE();//create randoms for each element

void Create_rand_cross();//choose crossover randoms

int find_cum1(float r);//find cumulative probability

void find_cumulative();//find new order of population

int Choose_Cross_Set(float PC);//chooses crossover sets

void Make_Cross_Over(int i,int ran);//make crossover

int Start_cross_over(float PC);//start crossing over

void find_best_S();//find the population with least cost

void Swap(int j);//swaps elements to make cross over

void Mutation(float PM);//do mutation

int Mark_S(float PM);//select elements which will undergo mutation

float find_S(float array[]);//calculate the total cost of chromosome straightforward

float find_S(int i);//another way of calculating total cost

void New_GEN();//make new generation order using cumulative probability

void Place_largest_S();  //replace highest value of cost with the least value of last immediate generation

void Replace_infeasibles();


ofstream fout("output\\final.txt");

//#define	ITERARION	 3000//u have to define it according to your GENERATION num
#define SIZEof		1500//total number of elements in all population
#define LS 		10
#define MAX_GEN 210

class chromosome{
    public:
	  int duration[7];//duration of a project activity
	  float cost[7];//contain (direct + indirect) cost
};

chromosome set[MAX_GEN];//this will always hold our current generation

int iteration;//number of generation take from user

clock_t t1, t2;

float QC[210];//20;  qumulitive probability
float RC[210];//20 rand cross
float PRO[210];//20 probability

float MAX=0.0;//saves the best cost
float MIN = 10000.0;//saves d least cost

int CS[210];//chosen crossover set
int G = 0;// times best value did not get better
float Lamda;

float RAND_MUTE[SIZEof];//NUM_OF_POPULATION*LENGTH_OF_CHROMOSOME random mutation numbers to select mutation element

int Selected[SIZEof];//20  NUM_OF_POPULATION selected elements to do mutation with

long int Best_duration[7];//

//long int Best_duration2[7];//L starts from 1;

float Best_cost[7];//saves costs of best populatation duration

int mut_Dur[7][50];//saves alternate mutation durations
float mut_Cost[7][50];//saves corresponding alternative mutation costs
int mut_size[7];


int L = 7; //length of chromosomes/schedule

int NUM_OF_POPULATION = 10; //number of chromosomes

int LENGTH_OF_CHROMOSOME=7; //length of chromosomes/schedules

int KC[MAX_GEN]; //this array saves selectd cross over

int d_constraint;// duration constraint



void Create_RAND_MUTE()//create random number for myration
{
   srand (time(NULL) );
     for(int i=0;i<LENGTH_OF_CHROMOSOME*NUM_OF_POPULATION;i++)
    {

        float j= (rand() %9)*10000+(rand() % 9)*1000+(rand() %9)*100+(rand() %9)*10+(rand() %9);
        RAND_MUTE[i]=j/100000;  //create random mutation number
    }
}
void Create_rand_cross()
{
    srand (time(NULL) );
     for(int i=0;i<NUM_OF_POPULATION;i++)
    {
        float j= (rand() %9)*10000+(rand() % 9)*1000+(rand() %9)*100+(rand() %9)*10+(rand() %9);
        RC[i]=j/100000;//create random crossover numbers to determine crossover sets pair

     //  cout<<RC[i]<<endl;
    }

}

void Create_Probability()
{       int i;
	    float P[210];//={0,.421,.395,.283,.293,.288,.356,.397,.307,.336,.404};
        //    	float P2[21];
        float sum=0;
        float sum2=0;
        for(i=0;i<NUM_OF_POPULATION;i++)
        {
            P[i] = find_S(i);
            sum = sum + P[i];
        }

      for(i=0;i<NUM_OF_POPULATION;i++)
      {
          PRO[i]=P[i]/sum;//create probability
          sum2 = sum2 + PRO[i];
          QC[i]=sum2;//create qumulative probability
      //    cout<<sum2<<endl;
      }
}

int find_cum1(float r)
{

    int i = NUM_OF_POPULATION-1;//
    while(i>0)
    {
        if(QC[i]>r)//find new sequence of population
        i--;
        else
        return i;
    }
    return i;
}
void find_cumulative()
{
     float Ra[210];
     srand (time(NULL) );
    //    cout<<endl<<"Ra"<<endl;

     for(int p=0;p < NUM_OF_POPULATION;p++)
    {
        float j= (rand() %9)*10000+(rand() % 9)*1000+(rand() %9)*100+(rand() %9)*10+(rand() %9);
        //float j= rand() % 8110+rand() % 1001+rand() %613+rand() % 98;
        Ra[p]=j/100000;
//       cout<<Ra[p]<<endl;
    }

    for(int i=0;i < NUM_OF_POPULATION;i++)
      {
          CS[i] = find_cum1(Ra[i]);//new order of populations
      }
}


int Choose_Cross_Set(float PC)
{
    int j=0;
    for(int i=0;i < NUM_OF_POPULATION;i++)
    {
        if(RC[i] <= PC)
        KC[j++] = i;
       // cout<<j;
    }
    if(j%2 != 0)
    {
        KC[j-1] = 0;//delete one it number of selected is odd
        return (j-1);
    }
    return j;//number is even return the umber
}
void Make_Cross_Over(int i,int ran)//everything is ready start crossing over
{
	int b = 0;
    int t;
	float temp;
    int j=0;
	if(ran > 0 && KC[i+1] < NUM_OF_POPULATION)
	{

		//cout<<endl;
				for(j = ran;j < LENGTH_OF_CHROMOSOME;j++)
				{
					t = set[KC[i]].duration[j];
					set[KC[i]].duration[j] = set[KC[i+1]].duration[j];
					set[KC[i+1]].duration[j] = t;

					temp = set[KC[i]].cost[j];
					set[KC[i]].cost[j] = set[KC[i+1]].cost[j];
					set[KC[i+1]].cost[j] = temp;

		//			cout<<"yaa"<<j<<" ";
				}
		//		cout<<endl;
				// demystifying(Sch[KC[i]].array);
				// if(check_valid()==1)
					//{
						//demystifying(Sch[KC[i+1]].array);
						//if(check_valid()==1)
						 //{
		//				  cout<<endl<<"yes crossovered"<<KC[i]<<endl;
						 // return;
						  //}
						//else
						//{
		//				   cout<<"oops"<<endl ;
							//for(j=b;j<LENGTH_OF_CHROMOSOME;j++)
								//{
									//t=Sch[KC[i]].array[j];
									//Sch[KC[i]].array[j]=Sch[KC[i+1]].array[j];
									//Sch[KC[i+1]].array[j]=t;
								//}
						 //}
					//}
        /*
			   else
				   {   //cout<<"oops"<<endl;
						   for(j=b;j<LENGTH_OF_CHROMOSOME;j++)
						{
							t=Sch[KC[i]].array[j];
							Sch[KC[i]].array[j]=Sch[KC[i+1]].array[j];
							Sch[KC[i+1]].array[j]=t;
						}
				   }
*/
	}
}
int Start_cross_over(float PC)
{
    New_GEN();//create new generation using cumulative probability
    int j = Choose_Cross_Set(PC);//choose crossover set
    int ran = 0;
    if(j == 0)
    {
    //  cout<<"sorry random is too random";
	//no population is selected
        return -1;
    }
    else
    {
          //cout<<"*--->j   "<<j;
        for(int i=0;i<j;i=i+2)
        {
            ran= rand() % LENGTH_OF_CHROMOSOME;//select the division point randomly
            // cout<<"ran"<<ran<<endl;;
            Make_Cross_Over(i,ran);// make cross over using that point
        }
    }
	/*
      for(int i=0;i<NUM_OF_POPULATION;i++)
          {
              cout<<i<<" ";
                for(j=0;j<LENGTH_OF_CHROMOSOME;j++)
           {
           //    cout<<Sch[i].array[j]<<" ";
           }
        //   cout<<"  "<<find_S(i) ;
         //  cout<<endl;
       }
	   */

}
void find_best_S()
{
    //float max = MAX;
    float min = MIN;
    int i = 0;
    int j = -1 ;
    for(i=0; i < NUM_OF_POPULATION; i++)
    {
        float m = find_S(i);
        if(m < min)
        {
            j = i;
            min = m;
        }
    }
    MIN = min;
    if(j > -1)
    {
        //cout<<"\n best s"<<j<<"\n";
            for(i=0;i < LENGTH_OF_CHROMOSOME;i++)  //L
        {
			Best_duration[i] = set[j].duration[i];
			Best_cost[i] = set[j].cost[i];

          // cout<<Best_duration[i]<<" ";
        }
        //cout<<"\n";
    }

}

void Swap(int j)
{

    for( int i=0; i<j ;i++ )
    {
        int select = Selected[i];
        //    cout<<"select:";
          //  cout<<endl<<select<<endl;
        int row  = select / LENGTH_OF_CHROMOSOME;//???????????
        int col  = select - row*LENGTH_OF_CHROMOSOME;
        //int a = Sch[row].array[col];
       // a/=100;
       // a%=100;
        if( col >= 0 )
        {

        //int b=Sch[row].array[col-1];
       // b/=100;
        //b%=100;
            //if(a==b)
           // {
		srand ( time(NULL) );
		int num = rand() % mut_size[col];

		//int t = set[row].duration[col];
		set[row].duration[col] = mut_Dur[col][num];//replace it from the array

		//float temp = set[row].cost[col];
		set[row].cost[col] = mut_Cost[col][num];//

                //int t = set[row].duration[col];
                //set[row].duration[col] = set[row].duration[col-1];
                //set[row].duration[col-1] = t;

                //float temp = set[row].cost[col];
                //set[row].cost[col] = set[row].cost[col-1];
                //set[row].cost[col-1] = temp;


                    ///demystifying(Sch[row].array);
                    //if(check_valid()==0)
                    ///{
                        //long t=Sch[row].array[col];
                       // Sch[row].array[col]=Sch[row].array[col-1];
                       // Sch[row].array[col-1]=t;
                    //}
                    //need to omit below
                    //else
                    //{
            /*            cout<<"yes"<<row<<" "<<col;
                          for(j=0;j<LENGTH_OF_CHROMOSOME;j++)
                           {

                               cout<<Sch[row].array[j]<<" ";
                           }
                        cout<<"  "<<find_S(row) ;
                        cout<<endl;
						*/

                    //}
            }
        }
   // }
/*    cout<<"\n after swapping \n";
              for(int f=0;f<NUM_OF_POPULATION;f++)
                  {
                        cout<<f<<" ";
                        for(j=0;j<LENGTH_OF_CHROMOSOME;j++)
                           {

                               cout<<Sch[f].array[j]<<" ";
                           }
                        cout<<"  "<<find_S(f) ;
                        cout<<endl;
                   }
  */

}
int find_max(int value[])
	{
	int max = value[0];
	for(int i=1;i<5;i++)
		if(max < value[i])
			max = value[i];

	return max;
	}
void replace(int k)
	{
	for(int i=0;i<LENGTH_OF_CHROMOSOME;i++)//L
            {
                set[k].duration[i] = Best_duration[i];
				set[k].cost[i] = Best_cost[i];

            }

	}

void Replace_infeasibles()
{
	int path[5],max;
	for(int i=0;i < NUM_OF_POPULATION;i++)
		{
		path[0] = set[i].duration[0] + set[i].duration[3] +set[i].duration[6];
				//A->D->G
		path[1] = set[i].duration[0] + set[i].duration[2] + set[i].duration[4] + set[i].duration[6];
				//A->C->E->G
		path[2] = set[i].duration[0] + set[i].duration[2] + set[i].duration[5];
				//A->C->F
		path[3] = set[i].duration[1] + set[i].duration[4] + set[i].duration[6];
				//B->E->G
		path[4] = set[i].duration[1] + set[i].duration[5];
				//B->F

		max = find_max(path);
		if(max > d_constraint)
			replace(i);

		}


}


void Mutation(float PM)
{

    Create_RAND_MUTE();
    int j = Mark_S(PM);
    Swap(j);

    Place_largest_S();
	Replace_infeasibles();
    find_best_S();
}
int Mark_S(float PM)
{

	int i;
    for(i=0; i < NUM_OF_POPULATION * LENGTH_OF_CHROMOSOME; i++)
       {
               Selected[i]=0;
       }
           int k=0;
    //cout<<"**marking**"<<endl;
    for(i=0; i < NUM_OF_POPULATION * LENGTH_OF_CHROMOSOME; i++)
       {
           if(RAND_MUTE[i] <= PM)
           {
               Selected[k++]=i;
           }
       }
    return k;
}

float find_S(float array[])
{
      float f = 0.0;//holds the total cost
      for(int j=0;j < LENGTH_OF_CHROMOSOME;j++)//L
	      f = f + array[j];//sum up all d cost into f and send it back that's our fitness function
      return f;
}
float find_S(int i)
    {
     float f = 0.0;
      for(int j=0;j < LENGTH_OF_CHROMOSOME;j++)
	  f = f + set[i].cost[j];
	return f;

}


void New_GEN()
{
    int i,j;
    int temp1[NUM_OF_POPULATION][LENGTH_OF_CHROMOSOME]; //temp array to store durations
    float temp2[NUM_OF_POPULATION][LENGTH_OF_CHROMOSOME]; //temp array to store costs
    Create_Probability();
    Create_rand_cross();
    find_cumulative()  ;
    for(i=0;i < NUM_OF_POPULATION;i++)
      {
          for( j=0;j < LENGTH_OF_CHROMOSOME;j++ )
             {
                    temp1[i][j] = set[i].duration[j];
		    temp2[i][j] = set[i].cost[j];

               //     cout<<T[i][j]<<" ";
             }
             //cout<<endl;
      }
       //cout<<"*new*"<<endl;
         for(i=0;i < NUM_OF_POPULATION;i++)
          {
                for(j=0; j < LENGTH_OF_CHROMOSOME; j++)
           {
			set[i].duration[j] = temp1[CS[i]][j];
			set[i].cost[j] = temp2[CS[i]][j];
           //      cout<<Sch[i].array[j]<<" ";
           }
          // cout<<" "<<find_S(i);
          // cout<<endl;
       }
}


void Place_largest_S()
{
	int i;
	float max = find_S(set[0].cost);//calculate cost of first population
	int k = 0;
    //cout<<"\n ^^now:^^^"<<endl;

    for(i=1; i < NUM_OF_POPULATION; i++)//calculate cost of all pupulations
    {
        float m = find_S(set[i].cost);
   /*         for(int e=0;e<TAM;e++)//L
        {
            cout<<Sch[i].array[e]<<" ";
        }
	*/
        //cout<<m<<endl;
        if(m > max )
        {
             max = m ;
             k = i;//k hold the index of worst population
        }
    }
    ///$$$$
   // cout<<min;
    //cout<<endl<<k;
     /*   for(i=0;i<TAM;i++)//L
    {
        cout<<Sch[k].array[i]<<" ";
    }
    cout<<"  "<<find_S(Sch[k].array);


    cout<<endl<<"final"<<endl;
    cout<<k<<" ";
       ///////$$$$$$$*/
       if( find_S(Best_cost) >= max)
       {
        G++; //increment G ;value is not upgraded
       }
       else
       {   G=0;
            for(i=0;i<LENGTH_OF_CHROMOSOME;i++)//L
            {
                set[k].duration[i] = Best_duration[i];
				set[k].cost[i] = Best_cost[i];

       //         cout<<Sch[k].array[i]<<" ";//$$$close
            }
         //   cout<<"  "<<find_S(k)<<endl<<endl;
            //int j;
            /*$$$$close
                  for(i=0;i<X;i++)
                  {
           //           cout<<i<<" ";
                        for(j=0;j<TAM;j++)
                   {

                       cout<<Sch[i].array[j]<<" ";
                   }
                   cout<<"  "<<find_S(i) ;
                   cout<<endl;
                   }
                   /////$$$$
				   */

	   }
}



//void demo()
  //  {
  //set[0].


//}



int main()
{
       int i,j,TA;
      // NUM_OF_POPULATION = 10;
      // LENGTH_OF_CHROMOSOME = 7;
       time_t start,end;
       float PC=.25,Pm=.15;
       int iteration=2500,iteration2=900;
       float alpha=.9;
       float Temp=2000;
       int countS=1000;
       int gg=5;
       float TS=20;

	   d_constraint  = 60;

       cout<<"NUMBER OF INITIAL POPULATION (<=200):\n";
	   cin>>NUM_OF_POPULATION;
	   //cout<<"NT\n";
	    //cin>>NT;

       cout << "NUMBER OF GANERATIONS:\n";
       cin>>iteration;
       //cout<<"LAST G TIMES BEST VALUE DIDNT CHANGED\n";
       //cin>>gg;
       cout<<"enter the value for Pc..crossing over constant:\n";
       cin>>PC;
       //cout<<"enter Lamda for GA(usually .35)  \n";
       // cin>>Lamda;

       cout<<"enter the value for Pm % Mutation rate: \n";
       cin>>Pm;
       
       cout<<"Enter the value for Constraint(same as previous one): \n";
       cin>>d_constraint;
	/*
	   NT=1.2;
	   Lamda=.35;
	   cout<<"machine";
       cin>>MACH[0];
       cout<<"job";
       cin>>JOB[0];

	   cout<<prompt1;
	   cin>>addr1;
	   cout<<prompt2;
	   cin>>addr2;
	   cout<<prompt3;
	   cin>>addr3;
	   cout<<prompt4;
	   cin>>addr4;


	   cout<<"NUMBER OF INITIAL POPULATION (<30)\n";
       cin>>X;
	   cout<<"NT\n";
       cin>>NT;

       cout<<"NUMBER OF GANERATIONS\n";
       cin>>iteration;
       cout<<"LAST G TIMES BEST VALUE DIDNT CHANGED\n";
       cin>>gg;
       cout<<"enter the value for Pc..crossing over constant\n";
       cin>>PC;
       cout<<"enter Lamda for GA(usually .35)  \n";
        cin>>Lamda;

       cout<<"enter the value for Pm % \n";
       cin>>Pm;
       cout<<"maximum iteration for simulated annealing  \n";
        cin>>iteration2;
        cout<<"alpha   \n";
        cin>>alpha;
        cout<<"enter Ts stopping condition of time  \n" ;
        cin>>TS;
        cout<<"enter initial temperature T    \n";
        cin>>Temp;
        cout<<"enter last N times didn't show better result   \n";
        cin>>countS;

		read_file();

        INIT();
        //take_input();
        find_array();
        calc_fact();
        TAM=L;

       // Demo();
        make_schdule();
       // cout<<"here sch";
     //   for(i=0;i<TAM;i++)
       // cout<<SCH[i]<<" ";
       // cout<<"\n\n";

*/
    int tempdur;
    float tempcost;
	ifstream in1("duration.txt");
	ifstream in2("cost.txt");

    if(!in1)
	  cout<<"Fatal error file duration.txt could not be opened";
    if(!in2)
          cout<<"Fatal error file cost.txt could not be opened";
         i =0;
        while(i < NUM_OF_POPULATION)
                {
            for(int j=0;j<7;j++)
                {
                in1>>tempdur;
                in2>>tempcost;
                set[i].duration[j] = tempdur;
                set[i].cost[j] = tempcost;

                    }

            i++;

                    }
	ifstream in3("duration_array.txt");
	ifstream in4("cost_array.txt");

	if(!in3 && !in4)
	    {
	    cout << "The alternate mutation files could not be opened.";
	    return 0;
		  }
	 j=0;

	float temp1;
	int temp2;
	while(j < LENGTH_OF_CHROMOSOME)
	    {
	    in3 >> mut_size[j];//input how much to read
	    for(i=0;i < mut_size[j];i++)
		    {
            //cout<<mut_size<<" "<<i<<" ";
		    in3 >> temp2;
		    in4 >> temp1;
		    //cout<<temp1<<" "<<temp2<<endl;
            mut_Dur[j][i] = temp2;

		    mut_Cost[j][i] = temp1;
			      }

		 j++;
}
        cout<<endl;
       // for(i=0;i<7;i++)
            //            cout<<mut_size[i]<<" ";
             ofstream out("output\\output.txt");
		//for(i = 0;i<LENGTH_OF_CHROMOSOME;i++)
				//for(j=0;j < mut_size[i];j++)
					//cout<<mut_Dur[i][j]<<" "<<mut_Cost[i][j]<<endl;

					//getch();

                    out<<"The initial population is:"<<endl;
          for(i=0;i<NUM_OF_POPULATION;i++)
            {
                for(j=0;j<7;j++)
                    out << set[i].duration[j]<<" ";
                out << endl;
            }
            
            t1 = clock();

		int IT = 1;
		find_best_S();
		Start_cross_over(PC);
            Mutation(Pm);
            fout<<endl<<"The generations and their corresponding best values are given below:"<<endl;
            while(IT < iteration) {

                     Start_cross_over(PC);
                     Mutation(Pm);

                     out << endl <<"After "<< IT <<" generation The population is: "<<endl<<endl;
                     for( i=0;i<NUM_OF_POPULATION;i++ )
                     {
                     for( j=0;j<7;j++ )
                                     out<<set[i].duration[j]<<" ";
				      out<<"    ---->"<<find_S(i);
                                     out<<endl;
                                     }
                     IT++;
                     //for(i=0;i<NUM_OF_POPULATION;i++)



					find_best_S();
                    fout<<endl<<"Generation "<<IT<<" :"<<endl;
					fout<<"Duration: ";
						for(i=0;i<7;i++)
							fout<<Best_duration[i]<<" ";
					fout<<endl<<"Cost: ";
						float joy = 0.0;
						for(i=0;i<7;i++)
							{
								joy += Best_cost[i];
								fout<<Best_cost[i]<<" ";
							}
					fout<<endl<<"The value in this generation is: "<<joy;


                     }
                     
                     t2 = clock();
				fout<<endl<<"The final best value is: "<<MIN;
				cout << "\n\nTotal time for GA execution is " << ((t2 - t1) / (CLK_TCK))  << " seconds plus " << ((t2 - t1) % (CLK_TCK)) << " milliseconds" << endl ;
				




/*
        Create_rand_cross();
        Create_New_Set();
       int x,l;
	find_Best_SEQ(TAM);
       find_best_S();

       t1= clock() ;
       Start_cross_over(PC);
       Mutation(Pm);

	   for(l=0;l<TAM;l++)
       Best_Array2[l]=Best_Array[l];
       WRITE[0]=find_S(Best_Array);

       j=1;
       while(G<=gg&&j<iteration)
       {
             // cout<<endl;
              Start_cross_over(PC);
          //    cout<<endl<<"********crossover finished******"<<j<<endl;
              Mutation(Pm);
        //      cout<<endl<<"######mutation finished#######"<<j<<endl;
              WRITE[j]=find_S(Best_Array);
              j++;
        }

		t2= clock();

        cout << "\n\nTotal time for GA execution is " << ((t2 - t1) / (CLK_TCK))  << " seconds plus " << ((t2 - t1) % (CLK_TCK)) << " milliseconds" << endl ;
        for(l=0;l<TAM;l++)
            cout<<Best_Array[l]<<" ";
            cout<<find_S(Best_Array);

         t3= clock();
        // SAAF(11,20,.9,.5,500,20);//11=L
        SAAF(TAM,iteration2,alpha,TS,Temp,countS);

		t5= clock() ;
		//cout<<t3<<"   "<<t4<<"\n";
		cout << "\n\nTotal time for SA execution is" << ((t5 - t3) / (CLK_TCK))  << " seconds plus " << ((t5 - t3) % (CLK_TCK)) << " milliseconds" << endl ;
            for(l=0;l<TAM;l++)
             cout<<SAA[l]<<" ";
             cout<<find_S(SAA);


		write_output(iteration);

/*
    long int ppp[11]={10101,20102,30102,40101,20201,10202,30203,30301,10303,20303,40302};
      cout<<"\n--->"<<find_S(ppp);

        cout<<"^^^^^^^^^"<<endl;
             for(j=0;j<=50;j++)//X
               {
                for(int ma=1;ma<=3;ma++)//MS
                {
                    for(i=0;i<A[ma];i++)
                    cout<<mac[j].set[ma][i]<<" ";
                        cout<<endl<<endl;

                }
                cout<<j<<endl;
                }


    /*           cout<<endl;
                     for(j=0;j<10;j++)
                {        cout<<"  "<<j<<endl;
                         for(l=0;l<11;l++)

                           cout<<Sch[j].array[l]<<" ";
                           cout<<"s="<<find_S(j);
                           cout<<endl;
                 }
   /*
  cout<<endl;
   for(i=0;i<TAM;i++)
       {
          cout<<J[i]<<" "<<M[i]<<" "<<S[i]<<endl;
          cout<<JS[J[i]][M[i]][S[i]].ST[0]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].ST[1]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].ST[2]<<"        ";

          cout<<JS[J[i]][M[i]][S[i]].PT[0]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].PT[1]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].PT[2]<<"        ";

          cout<<JS[J[i]][M[i]][S[i]].CT[0]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].CT[1]<<" ";
          cout<<JS[J[i]][M[i]][S[i]].CT[2]<<" "<<"\n\n";

        }


  //      cout<<calc_IT()<<"**";
    //    calc_Max_IT();

  //      cout<<"sum:::"<<find_sum_TFT();
       // cout<<calc_S_PI();
    //    cout<<endl<<calc_S_TFT();

//        cout<<"sat-->"<<calc_SAT()<<endl<<endl;
   /*     cout<<"final<<"<<find_S(1);
        cout<<endl;
        cout<<"final<<"<<find_S(2);

        for(i=1;i<=4;i++)
            {
                    for(j=0;j<3;j++)
                      cout<<cmax[i][j]<<" ";

                   cout<<endl;
            }

                cout<<endl<<endl;
                /*
        for(i=1;i<=4;i++)
            {
                    for(j=0;j<3;j++)
                      cout<<TFT[i][j]<<" ";

                   cout<<endl;
            }
               long int at[24]={40102,30101,10101,50104,40201,10202,30203,20204,50202,40305,10303,20303,30302,50301,40403,30405,10404,20402,50405,40102,30101,10101,50104};
       demystifying(at);
       cout<<"s0..."<<check_valid();
//
     */
    cout<<endl<<"Press any key to finish and see the output in file output.txt";
	getch();
	return 0;
}
